package com.ydd.controller.common;

import com.ydd.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
@Slf4j
public class ImgController {
    /**
     * 图片读取
     *
     * @param
     * @return
     */
    @GetMapping("/**/upload/{objectName}")
    public void backRead(@PathVariable String objectName, HttpServletResponse response)  {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet("https://ydd-13b.oss-cn-guangzhou.aliyuncs.com/" + objectName);
            CloseableHttpResponse execute = httpClient.execute(httpGet);
            InputStream content = execute.getEntity().getContent();
            IOUtils.copy(content, response.getOutputStream());


        } catch (Exception e) {
            log.error("图片获取失败！");
            throw new BaseException("图片获取失败！");
        }
    }
}
